var token = "";
var searchResult = document.getElementById("searchResult");
var searchNullP = document.getElementById("searchNullP");
var searchData = document.getElementById("searchData");
var sidList = document.getElementById("sidList");
var funName = "";
var pageNum = 1;
var itemNum = 20;
var countNum = 0;
var type = 1;

var textStr = "";
var isShishi = "";
var pageFlag = "";
var pageflag = "";

(function(e) {

	mui.init({
		pullRefresh: {
			container: '#list',
			down: {
				style: 'circle',
				callback: pulldownRefresh
			},
			up: {
				auto: true,
				contentrefresh: '正在加载...',
				callback: pullupRefresh
			}
		}
	});

	setPxPerRem();
	//	changeLineHeight();
	mui.plusReady(function() {
		textStr = getQueryString("textStr");
		isShishi = getQueryString("isShishi");
		pageflag = getQueryString("pageflag");

		if(!isEmpty(textStr)) {
			$("#queryType").html(textStr);
			funName = "pledge";
		}
	});
})(mui);

var isRefresh = false;

function pulldownRefresh() {
	removeLi("list");
	isRefresh = true;
	pageNum = 1;
	if(isHaveNet()) {
		getWarehouseIn();
	} else {
		mui.toast("无网络连接！");
	}
}

//上拉加载
var isLoad = false;

function pullupRefresh() {
	isLoad = true;
	if(isHaveNet()) {
		getWarehouseIn();
	} else {
		mui.toast("无网络连接！");
	}
}
var keyword = "";
var currentSid = "";
var currentWarehouse = "";

var count = 0;

function getWarehouseIn() {
	token = localStorage.getItem("token");
	if(isEmpty(token)) {
		mui.toast("登录认证失败，请退出后重试");
		return;
	}

	var p = {
		op: funName,
		token: token,
		data: {
			"state": "-1",
			"keyword": keyword,
			"page": itemNum,
			"perpage": pageNum
		}
	}

	var jsonData = {
		p: JSON.stringify(p)
	}

	if(checkParams(jsonData)) {
		mui.toast("查询失败！");
		return;
	}

	var opts = {
		url: '',
		addressUrl: "testline",
		requestType: "GET",
		pastDate: jsonData
	}

	crossDomainAjax(opts, function(data) {
		plus.nativeUI.closeWaiting();
		showEle(searchResult);
		if(!isEmpty(data)) {
			hiddenEle(searchNullP);
			showEle(searchData);
			if(isEmpty(data.list)) {
				$("#searchNull").html(keyword);
				hiddenEle(searchData);
				showEle(searchNullP);
				mui.toast("暂无数据");
			} else {
				var rows = data.list;
				count = 0 ;
				for(var i = 0; i < rows.length; i++) {
					var goods = rows[i].goods;
					for(var j = 0; j < goods.length; j++) {
						createItem(goods[j], j);
					}
				}
				$("#countNum").html(count);
			}

		} else {
			$("#searchNull").html(keyword);
			hiddenEle(searchData);
			showEle(searchNullP);
			mui.toast("暂无数据");
		}

		if(isRefresh) {
			mui('#list').pullRefresh().endPulldown();
			isRefresh = false;
		}

		if(isLoad) {
			isLoad = false;
			mui('#list').pullRefresh().endPullup((pageNum > countNum));
		}
		if(pageNum == 1) {
			mui('#list').pullRefresh().endPullup(false);
		}
		pageNum++;
	});
}
var currentId = "";
var currentCode = "";
//mui("#list").on("tap", "li", function() {
//	var checkBoxs = list.getElementsByTagName("input");
//	for(var i = 0; i < checkBoxs.length; i++) {
//		checkBoxs[i].checked = false;
//	}
//	var currentBox = this.getElementsByTagName("input")[0];
//	currentBox.checked = true;
//	currentId = this.id;
//	currentCode = this.className;
//});

//$("#seleceInfo").click(function() {
//	if(isEmpty(currentId)) {
//		mui.toast("请选择条目后在查看");
//		return;
//	}
//	var postdata = {
//		pageflag: "page_password.html",
//		sid: currentSid,
//		warehouse: currentWarehouse,
//		itemID: currentId,
//		type: type,
//		itemCode: currentCode
//	}
//
//	var change = {
//		pageUrl: "page_itemInfo.html",
//		postData: postdata,
//		aniShow: "fade-in"
//	}
//
//	pageChange(change);
//});

var insertDiv = document.getElementById("insertDiv");

function createItem(row, index) {
	var createLi = document.createElement("li");
	createLi.id = row.id;
	createLi.className = row.Code;
	var ischeck = "";
	if(index == 0) {
		currentId = row.id;
		ischeck = "checked='checked'";
	}
	var id = row.id;
	var Code = row.SIS_ProductName;
	var Status = row.state_format;
	var CangKu = row.SS_SecondCount;
	var order_sn = row.SIM_Code;
	var Unit = row.SS_SecondCountUnit;
	count = count + Math.round(CangKu);
	CangKu = CangKu.slice(0, CangKu.length-1);
	createLi.innerHTML = '<cite style="width: 1.6rem;">' + id + '</cite>' +
		'<cite style="width: 2.6rem;">' + Code + '</cite>' +
		'<cite style="width: 2.6rem;">' + Status + '</cite>' +
		'<cite style="width: 2.8rem;">' + CangKu + Unit + '</cite>' +
		'<cite style="width: 3.7rem;">' + order_sn + '</cite>';
	document.body.appendChild(createLi);
	insertBefore(createLi, insertDiv);
}

$("#queryBtn").click(function() {
	keyword = $("#searchInput").val().trim();
	if(isEmpty(keyword) && !isShishi) {
		mui.toast("请输入入库单号查询");
		return;
	}
	pulldownRefresh();
});